import { createRouter, createWebHistory } from 'vue-router'

// 创建布局组件
const AdminLayout = {
  template: '<router-view />'
}

const TeacherLayout = {
  template: '<router-view />'
}

const StudentLayout = {
  template: '<router-view />'
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    // 基础路由
    {
      path: '/test',
      name: 'test',
      component: () => import('../views/TestPage.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue')
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/HomeView.vue')
    },
    // 管理员路由
    {
      path: '/admin',
      redirect: '/admin/dashboard',
      component: AdminLayout,
      children: [
        {
          path: 'dashboard',
          name: 'adminDashboard',
          component: () => import('../views/admin/DashboardView.vue')
        },
        {
          path: 'teacher/list',
          name: 'teacherList',
          component: () => import('../views/admin/TeacherListView.vue')
        },
        {
          path: 'teacher/add',
          name: 'addTeacher',
          component: () => import('../views/admin/AddTeacherView.vue')
        },
        {
          path: 'student/list',
          name: 'studentList',
          component: () => import('../views/admin/StudentListView.vue')
        },
        {
          path: 'student/add',
          name: 'addStudent',
          component: () => import('../views/admin/AddStudentView.vue')
        },
        {
          path: 'user/list',
          name: 'userList',
          component: () => import('../views/admin/UserListView.vue')
        },
        {
          path: 'user/add',
          name: 'addUser',
          component: () => import('../views/admin/AddUserView.vue')
        },
        {
          path: 'user/edit',
          name: 'editUser',
          component: () => import('../views/admin/EditUserView.vue')
        },
        {
          path: 'change-password',
          name: 'adminChangePassword',
          component: () => import('../views/admin/ChangePasswordView.vue')
        },
        // 课程管理路由
        {
          path: 'course/list',
          name: 'courseList',
          component: () => import('../views/admin/CourseListView.vue')
        },
        {
          path: 'course/add',
          name: 'addCourse',
          component: () => import('../views/admin/AddCourseView.vue')
        },
        // 班级管理路由
        {
          path: 'class/list',
          name: 'classList',
          component: () => import('../views/admin/ClassListView.vue')
        },
        {
          path: 'class/add',
          name: 'addClass',
          component: () => import('../views/admin/AddClassView.vue')
        }
      ]
    },
    // 教师路由
    {
      path: '/teacher',
      redirect: '/teacher/dashboard',
      component: TeacherLayout,
      children: [
        {
          path: 'dashboard',
          name: 'teacherDashboard',
          component: () => import('../views/teacher/DashboardView.vue')
        },
        {
          path: 'question/list',
          name: 'questionList',
          component: () => import('../views/teacher/QuestionListView.vue')
        },
        {
          path: 'question/add',
          name: 'addQuestion',
          component: () => import('../views/teacher/AddQuestionView.vue')
        },
        {
          path: 'question/edit',
          name: 'editQuestion',
          component: () => import('../views/teacher/EditQuestionView.vue')
        },
        {
          path: 'exam/list',
          name: 'examList',
          component: () => import('../views/teacher/ExamListView.vue')
        },
        {
          path: 'exam/add',
          name: 'addExam',
          component: () => import('../views/teacher/AddExamView.vue')
        },
        {
          path: 'exam/edit/:id',
          name: 'editExam',
          component: () => import('../views/teacher/EditExamView.vue'),
          props: true
        },
        {
          path: 'change-password',
          name: 'teacherChangePassword',
          component: () => import('../views/admin/ChangePasswordView.vue')
        }
      ]
    },
    // 学生路由
    {
      path: '/student',
      redirect: '/student/dashboard',
      component: StudentLayout,
      children: [
        {
          path: 'dashboard',
          name: 'studentDashboard',
          component: () => import('../views/student/DashboardView.vue')
        },
        {
          path: 'my-exams',
          name: 'myExams',
          component: () => import('../views/student/MyExamsView.vue')
        },
        {
          path: 'exam/take/:id',
          name: 'examTake',
          component: () => import('../views/student/ExamTakingView.vue'),
          props: true
        },
        {
          path: 'score/list',
          name: 'studentScoreList',
          component: () => import('../views/student/ScoreListView.vue')
        },
        {
          path: 'course/selection',
          name: 'courseSelect',
          component: () => import('../views/student/CourseSelectView.vue')
        },
        {
          path: 'change-password',
          name: 'studentChangePassword',
          component: () => import('../views/admin/ChangePasswordView.vue')
        }
      ]
    }
  ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  next()
})

export default router
